#include "MatrixTransformationWindow.h"
#include "RenderWidget.h"
#include <QGroupBox>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStackedLayout>
#include <QPushButton>
#include <QIcon>

MatrixTransformationWindow::MatrixTransformationWindow(QWidget *parent)
    : QWidget(parent), render_(new RenderWidget(this))  // 使用 RenderWidget
{
    setupUi();
}

//构建 UI
void MatrixTransformationWindow::setupUi() {
    auto *mainLayout = new QHBoxLayout(this);

    // 控制区域
    auto *ctrl = new QGroupBox(tr("向量与矩阵变换"), this);
    auto *vlay = new QVBoxLayout(ctrl);

    // 设置输入控件
    leX1_ = new QLineEdit(); leY1_ = new QLineEdit();
    leX2_ = new QLineEdit(); leY2_ = new QLineEdit();
    leA11_ = new QLineEdit(); leA12_ = new QLineEdit();
    leA21_ = new QLineEdit(); leA22_ = new QLineEdit();
    btnAdd_ = new QPushButton(tr("加向量"));
    btnTransform_ = new QPushButton(tr("矩阵变换"));

    vlay->addWidget(new QLabel("v1 (x1, y1)"));
    vlay->addWidget(leX1_); vlay->addWidget(leY1_);
    vlay->addWidget(new QLabel("v2 (x2, y2)"));
    vlay->addWidget(leX2_); vlay->addWidget(leY2_);
    vlay->addSpacing(10);
    vlay->addWidget(new QLabel("矩阵 a11 a12; a21 a22"));
    vlay->addWidget(leA11_); vlay->addWidget(leA12_);
    vlay->addWidget(leA21_); vlay->addWidget(leA22_);
    vlay->addSpacing(10);
    vlay->addWidget(btnAdd_);
    vlay->addWidget(btnTransform_);
    vlay->addStretch();

    ctrl->setLayout(vlay);
    ctrl->setMaximumWidth(250);

    mainLayout->addWidget(ctrl);
    mainLayout->addWidget(render_, 1);  // 将 RenderWidget添加到布局
    setLayout(mainLayout);
    resize(1000, 600);

    connect(btnAdd_, &QPushButton::clicked, this, &MatrixTransformationWindow::onAddVector);
    connect(btnTransform_, &QPushButton::clicked, this, &MatrixTransformationWindow::onTransform);
}

//读取输入的两个向量
void MatrixTransformationWindow::onAddVector() {
    float x1 = leX1_->text().toFloat();
    float y1 = leY1_->text().toFloat();
    float x2 = leX2_->text().toFloat();
    float y2 = leY2_->text().toFloat();
    // 传递到 RenderWidget 进行渲染
    render_->showVectorSum(QPointF(x1, y1), QPointF(x2, y2));
}

//读取矩阵元素，封装为 QVector4D
void MatrixTransformationWindow::onTransform() {
    float a11 = leA11_->text().toFloat();
    float a12 = leA12_->text().toFloat();
    float a21 = leA21_->text().toFloat();
    float a22 = leA22_->text().toFloat();
    // 传递到 RenderWidget 进行矩阵变换渲染
    render_->showMatrixTransform(QVector4D(a11, a12, a21, a22));
}
